package avicit.bdp.dms.das.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.utils.BdpLogUtil;
import avicit.bdp.dms.das.dao.StorageGroupDao;
import avicit.bdp.dms.das.dto.StorageGroupDTO;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.rest.msg.QueryRespBean;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @修改记录：
 */
@Service
public class StorageGroupService extends BaseService<StorageGroupDao, StorageGroupDTO> {

    /**
     * 保存记录
     * @param storageGroup 存储组
     * @return 新增对象主键
     */
    public String insertStorageGroup(StorageGroupDTO storageGroup) {
        storageGroup.setId(ComUtil.getId());
        this.insertSelective(storageGroup);
        //记录日志
        BdpLogUtil.log4Insert(storageGroup);
        return storageGroup.getId();
    }

    /**
     * 更新记录
     * @param storageGroup 存储组
     */
    public void updateStorageGroup(StorageGroupDTO storageGroup) {
        updateByPrimaryKey(getUpdateDto(storageGroup));
    }



    /**
     * 内部方法，获取修改的dto对象
     *
     * @param storageGroup 存储组
     * @return 存储组
     */
    private StorageGroupDTO getUpdateDto(StorageGroupDTO storageGroup) {
        StorageGroupDTO oldDTO = selectByPrimaryKey(storageGroup.getId());
        if (oldDTO == null) {
            throw new BusinessException("数据不存在");
        }

        //记录日志
        BdpLogUtil.log4Update(storageGroup, oldDTO);

        oldDTO.setName(storageGroup.getName());

        return oldDTO;
    }

    /**
     * 删除记录
     * @param id 主键
     */
    public void deleteStorageGroupById(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException("删除失败！传入的参数主键为null");
        }
        //记录日志
        StorageGroupDTO storageGroup = selectByPrimaryKey(id);
        if (storageGroup == null) {
            throw new BusinessException("删除失败！对象不存在");
        }
        BdpLogUtil.log4Delete(storageGroup);
        //删除业务数据
        deleteByPrimaryKey(id);

    }

    /**
     * 分页查询
     * @param keyword 关键词
     * @param pageNo 当前页
     * @param pageSize 每页大小
     * @return 存储组集合
     */
    public QueryRespBean<StorageGroupDTO> getStorageGroupList(String keyword, Integer pageNo, Integer pageSize) {
        QueryRespBean<StorageGroupDTO> queryRespBean = new QueryRespBean<>();
        try {
            com.github.pagehelper.PageHelper.startPage(pageNo, pageSize);
            Page<StorageGroupDTO> dataList = this.mapper.getStorageGroupList(keyword);
            queryRespBean.setResult(dataList);

            return queryRespBean;
        } catch (Exception e) {
            throw new BusinessException(e.getMessage(), e);
        }
    }

    /**
     * 搜索查询
     * @param keyword 关键词
     * @return 存储组集合
     */
    public List<StorageGroupDTO> getStorageGroupListByKeyword(String keyword) {
        return this.mapper.getStorageGroupListByKeyword(keyword);
    }
}